########### R-script cleans and analyzes the data for the third study
########### Jonne Kamphorst
########### 13/01/2023




library(haven)
library(tidyverse)
library(stringr)
library(estimatr)
library(interflex)
library(ggeffects)
library(extrafont)
library(margins)
library(texreg)
library(gghighlight)
library(ggrepel)

ches_trend <- read_dta("data/1999-2019_CHES_dataset_means(v3).dta")
EES_2014 <- read_dta("data/EES 2014 ZA5160_v4-0-0.dta") # EES 2014. Have to use 14 because MII is asked in there


#font_import()
loadfonts(quiet = T, device = "pdf")
windowsFonts(Georgia = windowsFont("Georgia")) #load fonts for windows machines


## Set ggplot theme with the Georgia font
theme_set(theme_light(base_family = "Georgia", base_size = 12))
# Also add:  axis.text=element_text(size=11), legend.text=element_text(size=12)





######### Add the CHES codes for the voting questions ------------------------
#. These are the party codes for likely to vote on in the cells.
EES_2014$qpp5_ches <- NA #which party did you vote for?
EES_2014$qpp6_ches <- NA #which party would vote for?
EES_2014$qpp21_ches <- NA #Close to a particular party

#Austria
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1040520] <- 1302 # AT OVP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1040302] <- 1301 # AT SPO 
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1040423] <- 1309 # AT NEOS 
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1040110] <- 1304 # AT Grune 
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1040420] <- 1303 # AT FPO 
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1040600] <- 1307 # AT BZO

# Belgium (Flanders & Wallonia)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056325] <- 119 ##  BE PVDA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056521] <- 109 ##  BE CD&V
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056327] <- 103 ##  BE SPA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056421] <- 107 ##  BE VLD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056913] <- 110 ##  BE NVA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056112] <- 105 ##  BE Groen
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056711] <- 112 ##  BE VB
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056522] <- 108 #   BE cdH
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056322] <- 102 #   BE PS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056427] <- 106 #   BE MR
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1056111] <- 104 #   BE ECOLO

# Bulgaria (1100300, 1100601, 1100400 missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1100600] <- 2010 # BG GERB
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1100900] <- 2004 # BG DPS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1100700] <- 2007 # BG Attack
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1100602] <- 2015 # BG BBT
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1103001] <- 2016 # BG ABV
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1100001] <- 2002 # BG SDS

# Cyrpus (1196002 is missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1196711] <- 4001 # CY DISY
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1196422] <- 4004 # CY DIKO
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1196322] <- 4005 # CY EDEK
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1196321] <- 4003 # CY AKEL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1196110] <- 4006 # CY KOP

# Czech Republic (1203321 1203110 is missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1203523] <- 2104 #  CZ KDU-CSL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1203530] <- 2109 #  CZ TOP09
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1203320] <- 2101 #  CZ CSSD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1203413] <- 2102 #  CZ ODS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1203220] <- 2103 #  CZ KSCM
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1203413] <- 2111 #  CZ ANO2011
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1203953] <- 2113 #  CZ SVOBODNI

# Germany
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1276521] <- 301 # GE CDU
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1276320] <- 302 # GE SPD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1276420] <- 303 # GE FDP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1276113] <- 304 # GE Grunen
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1276321] <- 306 # GE Linke
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1276621] <- 310 # GE AfD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1276951] <- 311 # GE Piraten

# Denmark (FolkB is missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1208320] <- 201 # DK SD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1208420] <- 211 # DK V
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1208330] <- 206 # DK SF
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1208720] <- 215 # DK DF
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1208410] <- 202 # DK RV
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1208421] <- 218 # DK LA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1208620] <- 203 # DK KF

# Estonia (1233003 is missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1233613] <- 2201 # EE IRL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1233410] <- 2204 # EE SDE
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1233430] <- 2203 # EE ER
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1233411] <- 2202 # EE EK
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1233100] <- 2207 # EE EER

# Greece (1300116 is missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1300511] <- 402 # GR ND
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1300215] <- 403 # GR SYRIZA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1300313] <- 401 # GR PASOK
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1300611] <- 401 # GR ANEL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1300710] <- 415 # GR XA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1300225] <- 414 # GR DIMAR
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1300210] <- 404 # GR KKE
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1300323] <- 413 # GR Potami
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1300703] <- 410 # GR LAOS

# Spain 
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724610] <- 502 # ESP PP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724320] <- 501 # ESP PSOE
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724220] <- 504 # ESP IU
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724010] <- 523 # ESP UPyd
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724905] <- 511 # ESP ERC
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724310] <- 526 # ESP Cs
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724230] <- 525 # ESP Podemos
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724007] <- 505 # ESP CiU
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724902] <- 506 # ESP EAJ-PNV
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724908] <- 513 # ESP BNG
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1724907] <- 517 # ESP CC

# Finland
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1246620] <- 1402 # FI KOK
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1246520] <- 1409 # FI KD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1246320] <- 1401 # FI SDP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1246810] <- 1403 # FI KESK
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1246901] <- 1406 # FI RKP/SFP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1246110] <- 1408 # FI VIHR
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1246223] <- 1404 # FI VAS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1246820] <- 1405 # FI PS

# France (Please check for 1250223. 1250636, 1250233 are missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1250626] <- 609 # FR UMP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1250320] <- 602 # FR PS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1250720] <- 610 # FR FN
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1250110] <- 605 # FR EELV
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1250223] <- 601 # FR PCF
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1250336] <- 613 # FR MODEM

# Hungary
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1348700] <- 2308 # HU JOBBIK
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1348110] <- 2309 # HU LMP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1348421] <- 2302 # HU Fidesz
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1348220] <- 2301 # HU MSzP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1348120] <- 2310 # HU E14
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1348330] <- 2311 # HU DK

# Ireland
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1372520] <- 702 # IE FG
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1372320] <- 703 # IE Lab
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1372620] <- 701 # IE FF
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1372110] <- 705 # IE GP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1372951] <- 707 # IE SF
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1372220] <- 708 # IE SP

# Italy (1380902, 1380630 are missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1380331] <- 837 # IT PD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1380610] <- 815 # IT FI
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1380720] <- 811 # IT LN
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1380956] <- 845 # IT M5S
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1380523] <- 814 # IT UDC
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1380007] <- 838 # IT SEl
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1380633] <- 848 # IT NCD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1380631] <- 844 # IT FDL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1380958] <- 827 # IT SVP

# Lithuania (1440420 is missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1440620] <- 2506 # LT TS-LKD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1440320] <- 2501 # LT LSDP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1440421] <- 2518 # LT LRLS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1440322] <- 2516 # LT DP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1440621] <- 2515 # LT TT
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1440952] <- 2511 # LT LLRA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1440524] <- 2507 # LT LVZS

# Luxembourg (1442220 is missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1442520] <- 3801 # LU CSV
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1442320] <- 3804 # LU LSAP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1442420] <- 3803 # LU DP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1442113] <- 3802 # LU Greng
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1442222] <- 3806 # LU DL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1442951] <- 3805 # LU ADR

# Latvia (1428620 1428422 1428424 are missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1428610] <- 2412 # LV V
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1428317] <- 2410 # LV SDPS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1428723] <- 2406 # LV NA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1428110] <- 2405 # LV ZZS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1428901] <- 2402 # LV LKS 

# Malta (1470100 is missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1470300] <- 3701 # MT PL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1470500] <- 3702 # MT PN

# Netherlands (1528528 - please check. EES codes as coalition of 2 parties)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1528420] <- 1003 # NL VVD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1528320] <- 1002 # NL PvdA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1528600] <- 1017 # NL PVV
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1528220] <- 1014 # NL SP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1528521] <- 1001 # NL CDA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1528330] <- 1004 # NL D66
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1528526] <- 1016 # NL CU
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1528110] <- 1005 # NL GL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1528951] <- 1018 # NL PvdD

# Poland
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1616435] <- 2603 # PL PO
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1616811] <- 2606 # PL PSL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1616210] <- 2601 # PL SLD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1616436] <- 2605 # PL PiS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1616310] <- 2613 # PL RP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1616001] <- 2614 # PL KNP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1616002] <- 2616 # PL SP

# Portugal (1620314 - please check. EES codes as coalition of 2 parties)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1620313] <- 1206 # PT PSD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1620520] <- 1202 # PT PP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1620311] <- 1205 # PT PS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1620229] <- 1201 # PT CDU
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1620211] <- 1208 # PT BE
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1620110] <- 1209 # PT MPT

# Romania (1642700, 1642800 are missing. 1642502 is a coalition)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1642300] <- 2701 # RO PSD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1642401] <- 2705 # RO PNL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1642400] <- 2704 # RO PDL
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1642981] <- 2710 # RO PP-DD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1642900] <- 2706 # RO UDMR
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1642503] <- 2711 # RO PMP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1642600] <- 2702 # RO PC

# Sweden
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752320] <- 1602 # SE SAP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752620] <- 1605 # SE M
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752110] <- 1607 # SE MP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752420] <- 1604 # SE FP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752810] <- 1603 # SE C
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752700] <- 1610 # SE SD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752520] <- 1606 # SE KD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752220] <- 1601 # SE V
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752953] <- 1612 # SE FI
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1752000] <- 1611 # SE PP

# Slovenia (1705450, 1705421, 1705324, 1705710, 1705952 are missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1705340] <- 2914 # SI PS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1705320] <- 2902 # SI SDS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1705323] <- 2903 # SI SD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1705951] <- 2906 # SI DeSUS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1705522] <- 2905 # SI NSI
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1705521] <- 2904 # SI SLS

# Slovakia (1703222 is missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1703521] <- 2805 # SK KDH
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1703523] <- 2802 # SK SDKU-DS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1703954] <- 2804 # SK SMK-MKP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1703423] <- 2803 # SK Smer-SD
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1703610] <- 2815 # SK NOVA
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1703440] <- 2812 # SK SaS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1703620] <- 2814 # SK OLaNO
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1703955] <- 2813 # SK MH
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1703710] <- 2809 # SK SNS

# UK (1826210, 1826903, 1826724, 1826720 are missing)
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1826620] <- 1101 # UK Cons
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1826320] <- 1102 # UK Lab
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1826421] <- 1104 # UK LibDems
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1826110] <- 1107 # UK Green
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1826951] <- 1108 # UK UKIP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1826902] <- 1105 # UK SNP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1826901] <- 1106 # UK Plaid

# Croatia
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1191320] <- 3102 # HR SDP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1191412] <- 3105 # HR HNS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1191511] <- 3101 # HR HDZ
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1191613] <- 3109 # HR HSP
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1191410] <- 3104 # HR HSLS
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1191952] <- 3107 # HR HDSSB
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1191330] <- 3112 # HR HL-SR
EES_2014$qpp6_ches[EES_2014$qpp6_ees==1191110] <- 3114 # HR ORaH




#Austria
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1040520] <- 1302 # AT OVP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1040302] <- 1301 # AT SPO 
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1040423] <- 1309 # AT NEOS 
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1040110] <- 1304 # AT Grune 
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1040420] <- 1303 # AT FPO 
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1040600] <- 1307 # AT BZO

# Belgium (Flanders & Wallonia)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056325] <- 119 ##  BE PVDA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056521] <- 109 ##  BE CD&V
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056327] <- 103 ##  BE SPA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056421] <- 107 ##  BE VLD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056913] <- 110 ##  BE NVA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056112] <- 105 ##  BE Groen
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056711] <- 112 ##  BE VB
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056522] <- 108 #   BE cdH
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056322] <- 102 #   BE PS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056427] <- 106 #   BE MR
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1056111] <- 104 #   BE ECOLO

# Bulgaria (1100300, 1100601, 1100400 missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1100600] <- 2010 # BG GERB
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1100900] <- 2004 # BG DPS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1100700] <- 2007 # BG Attack
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1100602] <- 2015 # BG BBT
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1103001] <- 2016 # BG ABV
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1100001] <- 2002 # BG SDS

# Cyrpus (1196002 is missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1196711] <- 4001 # CY DISY
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1196422] <- 4004 # CY DIKO
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1196322] <- 4005 # CY EDEK
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1196321] <- 4003 # CY AKEL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1196110] <- 4006 # CY KOP

# Czech Republic (1203321 1203110 is missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1203523] <- 2104 #  CZ KDU-CSL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1203530] <- 2109 #  CZ TOP09
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1203320] <- 2101 #  CZ CSSD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1203413] <- 2102 #  CZ ODS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1203220] <- 2103 #  CZ KSCM
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1203413] <- 2111 #  CZ ANO2011
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1203953] <- 2113 #  CZ SVOBODNI

# Germany
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1276521] <- 301 # GE CDU
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1276320] <- 302 # GE SPD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1276420] <- 303 # GE FDP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1276113] <- 304 # GE Grunen
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1276321] <- 306 # GE Linke
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1276621] <- 310 # GE AfD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1276951] <- 311 # GE Piraten

# Denmark (FolkB is missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1208320] <- 201 # DK SD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1208420] <- 211 # DK V
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1208330] <- 206 # DK SF
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1208720] <- 215 # DK DF
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1208410] <- 202 # DK RV
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1208421] <- 218 # DK LA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1208620] <- 203 # DK KF

# Estonia (1233003 is missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1233613] <- 2201 # EE IRL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1233410] <- 2204 # EE SDE
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1233430] <- 2203 # EE ER
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1233411] <- 2202 # EE EK
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1233100] <- 2207 # EE EER

# Greece (1300116 is missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1300511] <- 402 # GR ND
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1300215] <- 403 # GR SYRIZA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1300313] <- 401 # GR PASOK
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1300611] <- 401 # GR ANEL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1300710] <- 415 # GR XA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1300225] <- 414 # GR DIMAR
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1300210] <- 404 # GR KKE
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1300323] <- 413 # GR Potami
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1300703] <- 410 # GR LAOS

# Spain 
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724610] <- 502 # ESP PP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724320] <- 501 # ESP PSOE
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724220] <- 504 # ESP IU
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724010] <- 523 # ESP UPyd
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724905] <- 511 # ESP ERC
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724310] <- 526 # ESP Cs
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724230] <- 525 # ESP Podemos
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724007] <- 505 # ESP CiU
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724902] <- 506 # ESP EAJ-PNV
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724908] <- 513 # ESP BNG
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1724907] <- 517 # ESP CC

# Finland
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1246620] <- 1402 # FI KOK
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1246520] <- 1409 # FI KD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1246320] <- 1401 # FI SDP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1246810] <- 1403 # FI KESK
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1246901] <- 1406 # FI RKP/SFP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1246110] <- 1408 # FI VIHR
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1246223] <- 1404 # FI VAS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1246820] <- 1405 # FI PS

# France (Please check for 1250223. 1250636, 1250233 are missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1250626] <- 609 # FR UMP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1250320] <- 602 # FR PS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1250720] <- 610 # FR FN
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1250110] <- 605 # FR EELV
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1250223] <- 601 # FR PCF
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1250336] <- 613 # FR MODEM

# Hungary
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1348700] <- 2308 # HU JOBBIK
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1348110] <- 2309 # HU LMP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1348421] <- 2302 # HU Fidesz
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1348220] <- 2301 # HU MSzP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1348120] <- 2310 # HU E14
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1348330] <- 2311 # HU DK

# Ireland
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1372520] <- 702 # IE FG
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1372320] <- 703 # IE Lab
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1372620] <- 701 # IE FF
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1372110] <- 705 # IE GP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1372951] <- 707 # IE SF
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1372220] <- 708 # IE SP

# Italy (1380902, 1380630 are missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1380331] <- 837 # IT PD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1380610] <- 815 # IT FI
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1380720] <- 811 # IT LN
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1380956] <- 845 # IT M5S
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1380523] <- 814 # IT UDC
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1380007] <- 838 # IT SEl
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1380633] <- 848 # IT NCD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1380631] <- 844 # IT FDL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1380958] <- 827 # IT SVP

# Lithuania (1440420 is missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1440620] <- 2506 # LT TS-LKD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1440320] <- 2501 # LT LSDP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1440421] <- 2518 # LT LRLS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1440322] <- 2516 # LT DP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1440621] <- 2515 # LT TT
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1440952] <- 2511 # LT LLRA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1440524] <- 2507 # LT LVZS

# Luxembourg (1442220 is missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1442520] <- 3801 # LU CSV
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1442320] <- 3804 # LU LSAP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1442420] <- 3803 # LU DP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1442113] <- 3802 # LU Greng
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1442222] <- 3806 # LU DL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1442951] <- 3805 # LU ADR

# Latvia (1428620 1428422 1428424 are missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1428610] <- 2412 # LV V
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1428317] <- 2410 # LV SDPS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1428723] <- 2406 # LV NA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1428110] <- 2405 # LV ZZS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1428901] <- 2402 # LV LKS 

# Malta (1470100 is missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1470300] <- 3701 # MT PL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1470500] <- 3702 # MT PN

# Netherlands (1528528 - please check. EES codes as coalition of 2 parties)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1528420] <- 1003 # NL VVD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1528320] <- 1002 # NL PvdA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1528600] <- 1017 # NL PVV
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1528220] <- 1014 # NL SP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1528521] <- 1001 # NL CDA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1528330] <- 1004 # NL D66
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1528526] <- 1016 # NL CU
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1528110] <- 1005 # NL GL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1528951] <- 1018 # NL PvdD

# Poland
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1616435] <- 2603 # PL PO
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1616811] <- 2606 # PL PSL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1616210] <- 2601 # PL SLD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1616436] <- 2605 # PL PiS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1616310] <- 2613 # PL RP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1616001] <- 2614 # PL KNP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1616002] <- 2616 # PL SP

# Portugal (1620314 - please check. EES codes as coalition of 2 parties)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1620313] <- 1206 # PT PSD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1620520] <- 1202 # PT PP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1620311] <- 1205 # PT PS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1620229] <- 1201 # PT CDU
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1620211] <- 1208 # PT BE
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1620110] <- 1209 # PT MPT

# Romania (1642700, 1642800 are missing. 1642502 is a coalition)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1642300] <- 2701 # RO PSD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1642401] <- 2705 # RO PNL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1642400] <- 2704 # RO PDL
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1642981] <- 2710 # RO PP-DD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1642900] <- 2706 # RO UDMR
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1642503] <- 2711 # RO PMP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1642600] <- 2702 # RO PC

# Sweden
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752320] <- 1602 # SE SAP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752620] <- 1605 # SE M
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752110] <- 1607 # SE MP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752420] <- 1604 # SE FP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752810] <- 1603 # SE C
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752700] <- 1610 # SE SD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752520] <- 1606 # SE KD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752220] <- 1601 # SE V
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752953] <- 1612 # SE FI
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1752000] <- 1611 # SE PP

# Slovenia (1705450, 1705421, 1705324, 1705710, 1705952 are missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1705340] <- 2914 # SI PS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1705320] <- 2902 # SI SDS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1705323] <- 2903 # SI SD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1705951] <- 2906 # SI DeSUS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1705522] <- 2905 # SI NSI
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1705521] <- 2904 # SI SLS

# Slovakia (1703222 is missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1703521] <- 2805 # SK KDH
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1703523] <- 2802 # SK SDKU-DS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1703954] <- 2804 # SK SMK-MKP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1703423] <- 2803 # SK Smer-SD
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1703610] <- 2815 # SK NOVA
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1703440] <- 2812 # SK SaS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1703620] <- 2814 # SK OLaNO
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1703955] <- 2813 # SK MH
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1703710] <- 2809 # SK SNS

# UK (1826210, 1826903, 1826724, 1826720 are missing)
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1826620] <- 1101 # UK Cons
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1826320] <- 1102 # UK Lab
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1826421] <- 1104 # UK LibDems
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1826110] <- 1107 # UK Green
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1826951] <- 1108 # UK UKIP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1826902] <- 1105 # UK SNP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1826901] <- 1106 # UK Plaid

# Croatia
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1191320] <- 3102 # HR SDP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1191412] <- 3105 # HR HNS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1191511] <- 3101 # HR HDZ
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1191613] <- 3109 # HR HSP
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1191410] <- 3104 # HR HSLS
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1191952] <- 3107 # HR HDSSB
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1191330] <- 3112 # HR HL-SR
EES_2014$qpp5_ches[EES_2014$qpp5_ees==1191110] <- 3114 # HR ORaH




#############Same again but for 21 Question

#Austria
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1040520] <- 1302 # AT OVP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1040302] <- 1301 # AT SPO 
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1040423] <- 1309 # AT NEOS 
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1040110] <- 1304 # AT Grune 
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1040420] <- 1303 # AT FPO 
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1040600] <- 1307 # AT BZO

# Belgium (Flanders & Wallonia)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056325] <- 119 ##  BE PVDA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056521] <- 109 ##  BE CD&V
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056327] <- 103 ##  BE SPA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056421] <- 107 ##  BE VLD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056913] <- 110 ##  BE NVA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056112] <- 105 ##  BE Groen
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056711] <- 112 ##  BE VB
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056522] <- 108 #   BE cdH
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056322] <- 102 #   BE PS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056427] <- 106 #   BE MR
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1056111] <- 104 #   BE ECOLO

# Bulgaria (1100300, 1100601, 1100400 missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1100600] <- 2010 # BG GERB
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1100900] <- 2004 # BG DPS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1100700] <- 2007 # BG Attack
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1100602] <- 2015 # BG BBT
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1103001] <- 2016 # BG ABV
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1100001] <- 2002 # BG SDS

# Cyrpus (1196002 is missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1196711] <- 4001 # CY DISY
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1196422] <- 4004 # CY DIKO
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1196322] <- 4005 # CY EDEK
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1196321] <- 4003 # CY AKEL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1196110] <- 4006 # CY KOP

# Czech Republic (1203321 1203110 is missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1203523] <- 2104 #  CZ KDU-CSL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1203530] <- 2109 #  CZ TOP09
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1203320] <- 2101 #  CZ CSSD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1203413] <- 2102 #  CZ ODS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1203220] <- 2103 #  CZ KSCM
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1203413] <- 2111 #  CZ ANO2011
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1203953] <- 2113 #  CZ SVOBODNI

# Germany
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1276521] <- 301 # GE CDU
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1276320] <- 302 # GE SPD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1276420] <- 303 # GE FDP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1276113] <- 304 # GE Grunen
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1276321] <- 306 # GE Linke
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1276621] <- 310 # GE AfD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1276951] <- 311 # GE Piraten

# Denmark (FolkB is missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1208320] <- 201 # DK SD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1208420] <- 211 # DK V
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1208330] <- 206 # DK SF
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1208720] <- 215 # DK DF
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1208410] <- 202 # DK RV
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1208421] <- 218 # DK LA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1208620] <- 203 # DK KF

# Estonia (1233003 is missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1233613] <- 2201 # EE IRL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1233410] <- 2204 # EE SDE
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1233430] <- 2203 # EE ER
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1233411] <- 2202 # EE EK
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1233100] <- 2207 # EE EER

# Greece (1300116 is missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1300511] <- 402 # GR ND
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1300215] <- 403 # GR SYRIZA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1300313] <- 401 # GR PASOK
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1300611] <- 401 # GR ANEL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1300710] <- 415 # GR XA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1300225] <- 414 # GR DIMAR
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1300210] <- 404 # GR KKE
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1300323] <- 413 # GR Potami
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1300703] <- 410 # GR LAOS

# Spain 
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724610] <- 502 # ESP PP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724320] <- 501 # ESP PSOE
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724220] <- 504 # ESP IU
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724010] <- 523 # ESP UPyd
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724905] <- 511 # ESP ERC
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724310] <- 526 # ESP Cs
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724230] <- 525 # ESP Podemos
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724007] <- 505 # ESP CiU
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724902] <- 506 # ESP EAJ-PNV
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724908] <- 513 # ESP BNG
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1724907] <- 517 # ESP CC

# Finland
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1246620] <- 1402 # FI KOK
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1246520] <- 1409 # FI KD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1246320] <- 1401 # FI SDP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1246810] <- 1403 # FI KESK
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1246901] <- 1406 # FI RKP/SFP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1246110] <- 1408 # FI VIHR
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1246223] <- 1404 # FI VAS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1246820] <- 1405 # FI PS

# France (Please check for 1250223. 1250636, 1250233 are missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1250626] <- 609 # FR UMP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1250320] <- 602 # FR PS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1250720] <- 610 # FR FN
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1250110] <- 605 # FR EELV
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1250223] <- 601 # FR PCF
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1250336] <- 613 # FR MODEM

# Hungary
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1348700] <- 2308 # HU JOBBIK
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1348110] <- 2309 # HU LMP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1348421] <- 2302 # HU Fidesz
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1348220] <- 2301 # HU MSzP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1348120] <- 2310 # HU E14
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1348330] <- 2311 # HU DK

# Ireland
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1372520] <- 702 # IE FG
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1372320] <- 703 # IE Lab
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1372620] <- 701 # IE FF
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1372110] <- 705 # IE GP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1372951] <- 707 # IE SF
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1372220] <- 708 # IE SP

# Italy (1380902, 1380630 are missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1380331] <- 837 # IT PD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1380610] <- 815 # IT FI
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1380720] <- 811 # IT LN
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1380956] <- 845 # IT M5S
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1380523] <- 814 # IT UDC
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1380007] <- 838 # IT SEl
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1380633] <- 848 # IT NCD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1380631] <- 844 # IT FDL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1380958] <- 827 # IT SVP

# Lithuania (1440420 is missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1440620] <- 2506 # LT TS-LKD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1440320] <- 2501 # LT LSDP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1440421] <- 2518 # LT LRLS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1440322] <- 2516 # LT DP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1440621] <- 2515 # LT TT
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1440952] <- 2511 # LT LLRA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1440524] <- 2507 # LT LVZS

# Luxembourg (1442220 is missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1442520] <- 3801 # LU CSV
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1442320] <- 3804 # LU LSAP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1442420] <- 3803 # LU DP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1442113] <- 3802 # LU Greng
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1442222] <- 3806 # LU DL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1442951] <- 3805 # LU ADR

# Latvia (1428620 1428422 1428424 are missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1428610] <- 2412 # LV V
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1428317] <- 2410 # LV SDPS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1428723] <- 2406 # LV NA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1428110] <- 2405 # LV ZZS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1428901] <- 2402 # LV LKS 

# Malta (1470100 is missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1470300] <- 3701 # MT PL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1470500] <- 3702 # MT PN

# Netherlands (1528528 - please check. EES codes as coalition of 2 parties)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1528420] <- 1003 # NL VVD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1528320] <- 1002 # NL PvdA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1528600] <- 1017 # NL PVV
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1528220] <- 1014 # NL SP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1528521] <- 1001 # NL CDA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1528330] <- 1004 # NL D66
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1528526] <- 1016 # NL CU
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1528110] <- 1005 # NL GL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1528951] <- 1018 # NL PvdD

# Poland
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1616435] <- 2603 # PL PO
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1616811] <- 2606 # PL PSL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1616210] <- 2601 # PL SLD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1616436] <- 2605 # PL PiS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1616310] <- 2613 # PL RP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1616001] <- 2614 # PL KNP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1616002] <- 2616 # PL SP

# Portugal (1620314 - please check. EES codes as coalition of 2 parties)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1620313] <- 1206 # PT PSD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1620520] <- 1202 # PT PP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1620311] <- 1205 # PT PS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1620229] <- 1201 # PT CDU
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1620211] <- 1208 # PT BE
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1620110] <- 1209 # PT MPT

# Romania (1642700, 1642800 are missing. 1642502 is a coalition)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1642300] <- 2701 # RO PSD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1642401] <- 2705 # RO PNL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1642400] <- 2704 # RO PDL
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1642981] <- 2710 # RO PP-DD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1642900] <- 2706 # RO UDMR
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1642503] <- 2711 # RO PMP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1642600] <- 2702 # RO PC

# Sweden
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752320] <- 1602 # SE SAP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752620] <- 1605 # SE M
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752110] <- 1607 # SE MP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752420] <- 1604 # SE FP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752810] <- 1603 # SE C
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752700] <- 1610 # SE SD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752520] <- 1606 # SE KD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752220] <- 1601 # SE V
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752953] <- 1612 # SE FI
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1752000] <- 1611 # SE PP

# Slovenia (1705450, 1705421, 1705324, 1705710, 1705952 are missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1705340] <- 2914 # SI PS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1705320] <- 2902 # SI SDS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1705323] <- 2903 # SI SD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1705951] <- 2906 # SI DeSUS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1705522] <- 2905 # SI NSI
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1705521] <- 2904 # SI SLS

# Slovakia (1703222 is missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1703521] <- 2805 # SK KDH
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1703523] <- 2802 # SK SDKU-DS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1703954] <- 2804 # SK SMK-MKP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1703423] <- 2803 # SK Smer-SD
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1703610] <- 2815 # SK NOVA
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1703440] <- 2812 # SK SaS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1703620] <- 2814 # SK OLaNO
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1703955] <- 2813 # SK MH
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1703710] <- 2809 # SK SNS

# UK (1826210, 1826903, 1826724, 1826720 are missing)
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1826620] <- 1101 # UK Cons
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1826320] <- 1102 # UK Lab
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1826421] <- 1104 # UK LibDems
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1826110] <- 1107 # UK Green
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1826951] <- 1108 # UK UKIP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1826902] <- 1105 # UK SNP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1826901] <- 1106 # UK Plaid

# Croatia
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1191320] <- 3102 # HR SDP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1191412] <- 3105 # HR HNS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1191511] <- 3101 # HR HDZ
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1191613] <- 3109 # HR HSP
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1191410] <- 3104 # HR HSLS
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1191952] <- 3107 # HR HDSSB
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1191330] <- 3112 # HR HL-SR
EES_2014$qpp21_ches[EES_2014$qpp21_ees==1191110] <- 3114 # HR ORaH




################### Add whether a voter would consider a party and add the party codes for the thermostat questions ------------------------------------

# Work with people who at least answer the 'which party would you vote for question'
EES_2014 <- EES_2014 %>% drop_na(qpp6) %>% filter(qpp6 > 0)


#get rid of the NAs in the 8 question (thermostat Q)
EES_2014$qpp8_1[EES_2014$qpp8_1 == -99 | EES_2014$qpp8_1 == -9 | EES_2014$qpp8_1  == -8 | EES_2014$qpp8_1 == -7] <- NA
EES_2014$qpp8_2[EES_2014$qpp8_2 == -99 | EES_2014$qpp8_1 == -9 | EES_2014$qpp8_1  == -8 | EES_2014$qpp8_1 == -7] <- NA
EES_2014$qpp8_3[EES_2014$qpp8_3 == -99 | EES_2014$qpp8_1 == -9 | EES_2014$qpp8_1  == -8 | EES_2014$qpp8_1 == -7] <- NA
EES_2014$qpp8_4[EES_2014$qpp8_4 == -99 | EES_2014$qpp8_1 == -9 | EES_2014$qpp8_1  == -8 | EES_2014$qpp8_1 == -7] <- NA
EES_2014$qpp8_5[EES_2014$qpp8_5 == -99 | EES_2014$qpp8_1 == -9 | EES_2014$qpp8_1  == -8 | EES_2014$qpp8_1 == -7] <- NA
EES_2014$qpp8_6[EES_2014$qpp8_6 == -99 | EES_2014$qpp8_1 == -9 | EES_2014$qpp8_1  == -8 | EES_2014$qpp8_1 == -7] <- NA
EES_2014$qpp8_7[EES_2014$qpp8_7 == -99 | EES_2014$qpp8_1 == -9 | EES_2014$qpp8_1  == -8 | EES_2014$qpp8_1 == -7] <- NA
EES_2014$qpp8_8[EES_2014$qpp8_8 == -99 | EES_2014$qpp8_1 == -9 | EES_2014$qpp8_1  == -8 | EES_2014$qpp8_1 == -7] <- NA

EES_2014$chess_8_1 <- NA
EES_2014$chess_8_2 <- NA
EES_2014$chess_8_3 <- NA
EES_2014$chess_8_4 <- NA
EES_2014$chess_8_5 <- NA
EES_2014$chess_8_6 <- NA
EES_2014$chess_8_7 <- NA
EES_2014$chess_8_8 <- NA


attach(EES_2014)
#Austria
EES_2014$lv_ches_1302 <- ifelse(countrycode==1040 & qpp8_1 > 5 | qpp5_ches == 1302 | qpp6_ches == 1302 | qpp21_ches == 1302, 1, 0)
EES_2014$lv_ches_1301 <- ifelse(countrycode==1040 & qpp8_2 > 5 | qpp5_ches == 1301 | qpp6_ches == 1301 | qpp21_ches == 1301, 1, 0)
EES_2014$lv_ches_1309 <- ifelse(countrycode==1040 & qpp8_3 > 5 | qpp5_ches == 1309 | qpp6_ches == 1309 | qpp21_ches == 1309, 1, 0)
EES_2014$lv_ches_1304 <- ifelse(countrycode==1040 & qpp8_4 > 5 | qpp5_ches == 1304 | qpp6_ches == 1304 | qpp21_ches == 1304, 1, 0)
EES_2014$lv_ches_1303 <- ifelse(countrycode==1040 & qpp8_5 > 5 | qpp5_ches == 1303 | qpp6_ches == 1303 | qpp21_ches == 1303, 1, 0)
EES_2014$lv_ches_1307 <- ifelse(countrycode==1040 & qpp8_6 > 5 | qpp5_ches == 1307 | qpp6_ches == 1307 | qpp21_ches == 1307, 1, 0)

EES_2014$chess_8_1[countrycode==1040] <- 1302
EES_2014$chess_8_2[countrycode==1040] <- 1301
EES_2014$chess_8_3[countrycode==1040] <- 1309
EES_2014$chess_8_4[countrycode==1040] <- 1304
EES_2014$chess_8_5[countrycode==1040] <- 1303
EES_2014$chess_8_6[countrycode==1040] <- 1307


#Belgium (French)
EES_2014$lv_ches_108 <- ifelse(countrycode==1056 & p13_intlang==8 & qpp8_1 > 5 | qpp5_ches == 108 | qpp6_ches == 108 | qpp21_ches == 108, 1, 0)
EES_2014$lv_ches_102 <- ifelse(countrycode==1056 & p13_intlang==8 & qpp8_2 > 5 | qpp5_ches == 102 | qpp6_ches == 102 | qpp21_ches == 102, 1, 0)
EES_2014$lv_ches_106 <- ifelse(countrycode==1056 & p13_intlang==8 & qpp8_3 > 5 | qpp5_ches == 106 | qpp6_ches == 106 | qpp21_ches == 106, 1, 0)
EES_2014$lv_ches_104 <- ifelse(countrycode==1056 & p13_intlang==8 & qpp8_4 > 5 | qpp5_ches == 104 | qpp6_ches == 104 | qpp21_ches == 104, 1, 0)
EES_2014$lv_ches_120 <- ifelse(countrycode==1056 & p13_intlang==8 & qpp8_5 > 5 | qpp5_ches == 120 | qpp6_ches == 120 | qpp21_ches == 120, 1, 0)
EES_2014$lv_ches_119 <- ifelse(countrycode==1056 & p13_intlang==8 & qpp8_6 > 5 | qpp5_ches == 119 | qpp6_ches == 119 | qpp21_ches == 119, 1, 0)

EES_2014$chess_8_1[countrycode==1056] <- 108
EES_2014$chess_8_2[countrycode==1056] <- 102
EES_2014$chess_8_3[countrycode==1056] <- 106
EES_2014$chess_8_4[countrycode==1056] <- 104
EES_2014$chess_8_5[countrycode==1056] <- 120
EES_2014$chess_8_6[countrycode==1056] <- 119


#Belgium (Flanders)
EES_2014$lv_ches_119 <- ifelse(countrycode==1056 & qpp8_1 > 5 | qpp5_ches == 119 | qpp6_ches == 119 | qpp21_ches == 119, 1, EES_2014$lv_ches_119) #note not NA because double variable
EES_2014$lv_ches_109 <- ifelse(countrycode==1056 & qpp8_2 > 5 | qpp5_ches == 109 | qpp6_ches == 109 | qpp21_ches == 109, 1, 0)
EES_2014$lv_ches_103 <- ifelse(countrycode==1056 & qpp8_3 > 5 | qpp5_ches == 103 | qpp6_ches == 103 | qpp21_ches == 103, 1, 0)
EES_2014$lv_ches_107 <- ifelse(countrycode==1056 & qpp8_4 > 5 | qpp5_ches == 107 | qpp6_ches == 107 | qpp21_ches == 107, 1, 0)
EES_2014$lv_ches_110 <- ifelse(countrycode==1056 & qpp8_5 > 5 | qpp5_ches == 110 | qpp6_ches == 110 | qpp21_ches == 110, 1, 0)
EES_2014$lv_ches_105 <- ifelse(countrycode==1056 & qpp8_6 > 5 | qpp5_ches == 105 | qpp6_ches == 105 | qpp21_ches == 105, 1, 0)
EES_2014$lv_ches_112 <- ifelse(countrycode==1056 & qpp8_7 > 5 | qpp5_ches == 112 | qpp6_ches == 112 | qpp21_ches == 112, 1, 0)

EES_2014$chess_8_1[countrycode==1056] <- 119
EES_2014$chess_8_2[countrycode==1056] <- 109
EES_2014$chess_8_3[countrycode==1056] <- 103
EES_2014$chess_8_4[countrycode==1056] <- 107
EES_2014$chess_8_5[countrycode==1056] <- 110
EES_2014$chess_8_6[countrycode==1056] <- 105
EES_2014$chess_8_7[countrycode==1056] <- 112

#Germany
EES_2014$lv_ches_301 <- ifelse(countrycode==1276 & qpp8_1 > 5 | qpp5_ches == 301 | qpp6_ches == 301 | qpp21_ches == 301, 1, 0)
EES_2014$lv_ches_302 <- ifelse(countrycode==1276 & qpp8_2 > 5 | qpp5_ches == 302 | qpp6_ches == 302 | qpp21_ches == 302, 1, 0)
EES_2014$lv_ches_303 <- ifelse(countrycode==1276 & qpp8_3 > 5 | qpp5_ches == 303 | qpp6_ches == 303 | qpp21_ches == 303, 1, 0)
EES_2014$lv_ches_304 <- ifelse(countrycode==1276 & qpp8_4 > 5 | qpp5_ches == 304 | qpp6_ches == 304 | qpp21_ches == 304, 1, 0)
EES_2014$lv_ches_306 <- ifelse(countrycode==1276 & qpp8_5 > 5 | qpp5_ches == 306 | qpp6_ches == 306 | qpp21_ches == 306, 1, 0)
EES_2014$lv_ches_310 <- ifelse(countrycode==1276 & qpp8_6 > 5 | qpp5_ches == 310 | qpp6_ches == 310 | qpp21_ches == 310, 1, 0)
EES_2014$lv_ches_311 <- ifelse(countrycode==1276 & qpp8_7 > 5 | qpp5_ches == 311 | qpp6_ches == 311 | qpp21_ches == 311, 1, 0)

EES_2014$chess_8_1[countrycode==1276] <- 301
EES_2014$chess_8_2[countrycode==1276] <- 302
EES_2014$chess_8_3[countrycode==1276] <- 303
EES_2014$chess_8_4[countrycode==1276] <- 304
EES_2014$chess_8_5[countrycode==1276] <- 306
EES_2014$chess_8_6[countrycode==1276] <- 310
EES_2014$chess_8_7[countrycode==1276] <- 311

# Denmark
EES_2014$lv_ches_201 <- ifelse(countrycode==1208 & qpp8_1 > 5 | qpp5_ches == 201 | qpp6_ches == 201 | qpp21_ches == 201, 1, 0)
EES_2014$lv_ches_211 <- ifelse(countrycode==1208 & qpp8_2 > 5 | qpp5_ches == 211 | qpp6_ches == 211 | qpp21_ches == 211, 1, 0)
EES_2014$lv_ches_206 <- ifelse(countrycode==1208 & qpp8_3 > 5 | qpp5_ches == 206 | qpp6_ches == 206 | qpp21_ches == 206, 1, 0)
EES_2014$lv_ches_215 <- ifelse(countrycode==1208 & qpp8_4 > 5 | qpp5_ches == 215 | qpp6_ches == 215 | qpp21_ches == 215, 1, 0)
EES_2014$lv_ches_202 <- ifelse(countrycode==1208 & qpp8_5 > 5 | qpp5_ches == 202 | qpp6_ches == 202 | qpp21_ches == 202, 1, 0)
EES_2014$lv_ches_218 <- ifelse(countrycode==1208 & qpp8_6 > 5 | qpp5_ches == 218 | qpp6_ches == 218 | qpp21_ches == 218, 1, 0)
EES_2014$lv_ches_203 <- ifelse(countrycode==1208 & qpp8_7 > 5 | qpp5_ches == 203 | qpp6_ches == 203 | qpp21_ches == 203, 1, 0)
EES_2014$lv_ches_217 <- ifelse(countrycode==1208 & qpp8_8 > 5 | qpp5_ches == 217 | qpp6_ches == 217 | qpp21_ches == 217, 1, 0)

EES_2014$chess_8_1[countrycode==1208] <- 201
EES_2014$chess_8_2[countrycode==1208] <- 211
EES_2014$chess_8_3[countrycode==1208] <- 206
EES_2014$chess_8_4[countrycode==1208] <- 215
EES_2014$chess_8_5[countrycode==1208] <- 202
EES_2014$chess_8_6[countrycode==1208] <- 218
EES_2014$chess_8_7[countrycode==1208] <- 203
EES_2014$chess_8_8[countrycode==1208] <- 217


#Spain
EES_2014$lv_ches_502 <- ifelse(countrycode==1724 & qpp8_1 > 5 | qpp5_ches == 502 | qpp6_ches == 502 | qpp21_ches == 502, 1, 0)
EES_2014$lv_ches_501 <- ifelse(countrycode==1724 & qpp8_2 > 5 | qpp5_ches == 501 | qpp6_ches == 501 | qpp21_ches == 501, 1, 0)
EES_2014$lv_ches_504 <- ifelse(countrycode==1724 & qpp8_3 > 5 | qpp5_ches == 504 | qpp6_ches == 504 | qpp21_ches == 504, 1, 0)
EES_2014$lv_ches_523 <- ifelse(countrycode==1724 & qpp8_4 > 5 | qpp5_ches == 523 | qpp6_ches == 523 | qpp21_ches == 523, 1, 0)
EES_2014$lv_ches_505 <- ifelse(countrycode==1724 & qpp8_5 > 5 | qpp5_ches == 505 | qpp6_ches == 505 | qpp21_ches == 505, 1, 0) #is also 506 and 517
EES_2014$lv_ches_511 <- ifelse(countrycode==1724 & qpp8_6 > 5 | qpp5_ches == 511 | qpp6_ches == 511 | qpp21_ches == 511, 1, 0)
EES_2014$lv_ches_525 <- ifelse(countrycode==1724 & qpp8_7 > 5 | qpp5_ches == 526 | qpp6_ches == 526 | qpp21_ches == 526, 1, 0)
EES_2014$lv_ches_525 <- ifelse(countrycode==1724 & qpp8_8 > 5 | qpp5_ches == 525 | qpp6_ches == 525 | qpp21_ches == 525, 1, 0)

EES_2014$chess_8_1[countrycode==1724] <- 502
EES_2014$chess_8_2[countrycode==1724] <- 501
EES_2014$chess_8_3[countrycode==1724] <- 504
EES_2014$chess_8_4[countrycode==1724] <- 523
EES_2014$chess_8_5[countrycode==1724] <- 505
EES_2014$chess_8_6[countrycode==1724] <- 511
EES_2014$chess_8_7[countrycode==1724] <- 526
EES_2014$chess_8_8[countrycode==1724] <- 525


#Finland
EES_2014$lv_ches_1402 <- ifelse(countrycode==1246 & qpp8_1 > 5 | qpp5_ches == 1402 | qpp6_ches == 1402 | qpp21_ches == 1402, 1, 0)
EES_2014$lv_ches_1409 <- ifelse(countrycode==1246 & qpp8_2 > 5 | qpp5_ches == 1409 | qpp6_ches == 1409 | qpp21_ches == 1409, 1, 0)
EES_2014$lv_ches_1401 <- ifelse(countrycode==1246 & qpp8_3 > 5 | qpp5_ches == 1401 | qpp6_ches == 1401 | qpp21_ches == 1401, 1, 0)
EES_2014$lv_ches_1403 <- ifelse(countrycode==1246 & qpp8_4 > 5 | qpp5_ches == 1403 | qpp6_ches == 1403 | qpp21_ches == 1403, 1, 0)
EES_2014$lv_ches_1406 <- ifelse(countrycode==1246 & qpp8_5 > 5 | qpp5_ches == 1406 | qpp6_ches == 1406 | qpp21_ches == 1406, 1, 0)
EES_2014$lv_ches_1408 <- ifelse(countrycode==1246 & qpp8_6 > 5 | qpp5_ches == 1408 | qpp6_ches == 1408 | qpp21_ches == 1408, 1, 0)
EES_2014$lv_ches_1404 <- ifelse(countrycode==1246 & qpp8_7 > 5 | qpp5_ches == 1404 | qpp6_ches == 1404 | qpp21_ches == 1404, 1, 0)
EES_2014$lv_ches_1405 <- ifelse(countrycode==1246 & qpp8_8 > 5 | qpp5_ches == 1405 | qpp6_ches == 1405 | qpp21_ches == 1405, 1, 0)

EES_2014$chess_8_1[countrycode==1246] <- 1402
EES_2014$chess_8_2[countrycode==1246] <- 1409
EES_2014$chess_8_3[countrycode==1246] <- 1401
EES_2014$chess_8_4[countrycode==1246] <- 1403
EES_2014$chess_8_5[countrycode==1246] <- 1406
EES_2014$chess_8_6[countrycode==1246] <- 1408
EES_2014$chess_8_7[countrycode==1246] <- 1404
EES_2014$chess_8_8[countrycode==1246] <- 1405


#France
EES_2014$lv_ches_609 <- ifelse(countrycode==1250 & qpp8_1 > 5 | qpp5_ches == 609 | qpp6_ches == 609 | qpp21_ches == 609, 1, 0)
EES_2014$lv_ches_602 <- ifelse(countrycode==1250 & qpp8_2 > 5 | qpp5_ches == 602 | qpp6_ches == 602 | qpp21_ches == 602, 1, 0)
EES_2014$lv_ches_610 <- ifelse(countrycode==1250 & qpp8_3 > 5 | qpp5_ches == 610 | qpp6_ches == 610 | qpp21_ches == 610, 1, 0)
EES_2014$lv_ches_605 <- ifelse(countrycode==1250 & qpp8_4 > 5 | qpp5_ches == 605 | qpp6_ches == 605 | qpp21_ches == 605, 1, 0)
EES_2014$lv_ches_601 <- ifelse(countrycode==1250 & qpp8_5 > 5 | qpp5_ches == 601 | qpp6_ches == 601 | qpp21_ches == 601, 1, 0)
EES_2014$lv_ches_613 <- ifelse(countrycode==1250 & qpp8_6 > 5 | qpp5_ches == 613 | qpp6_ches == 613 | qpp21_ches == 613, 1, 0)

EES_2014$chess_8_1[countrycode==1250] <- 609
EES_2014$chess_8_2[countrycode==1250] <- 602
EES_2014$chess_8_3[countrycode==1250] <- 610
EES_2014$chess_8_4[countrycode==1250] <- 605
EES_2014$chess_8_5[countrycode==1250] <- 601
EES_2014$chess_8_6[countrycode==1250] <- 613


#Ireland
EES_2014$lv_ches_702 <- ifelse(countrycode==1372 & qpp8_1 > 5 | qpp5_ches == 702 | qpp6_ches == 702 | qpp21_ches == 702, 1, 0)
EES_2014$lv_ches_703 <- ifelse(countrycode==1372 & qpp8_2 > 5 | qpp5_ches == 703 | qpp6_ches == 703 | qpp21_ches == 703, 1, 0)
EES_2014$lv_ches_701 <- ifelse(countrycode==1372 & qpp8_3 > 5 | qpp5_ches == 701 | qpp6_ches == 701 | qpp21_ches == 701, 1, 0)
EES_2014$lv_ches_705 <- ifelse(countrycode==1372 & qpp8_4 > 5 | qpp5_ches == 705 | qpp6_ches == 705 | qpp21_ches == 705, 1, 0)
EES_2014$lv_ches_707 <- ifelse(countrycode==1372 & qpp8_5 > 5 | qpp5_ches == 707 | qpp6_ches == 707 | qpp21_ches == 707, 1, 0)
EES_2014$lv_ches_708 <- ifelse(countrycode==1372 & qpp8_6 > 5 | qpp5_ches == 708 | qpp6_ches == 708 | qpp21_ches == 708, 1, 0)

EES_2014$chess_8_1[countrycode==1372] <- 702
EES_2014$chess_8_2[countrycode==1372] <- 703
EES_2014$chess_8_3[countrycode==1372] <- 701
EES_2014$chess_8_4[countrycode==1372] <- 705
EES_2014$chess_8_5[countrycode==1372] <- 707
EES_2014$chess_8_6[countrycode==1372] <- 708


#Italy
EES_2014$lv_ches_837 <- ifelse(countrycode==1380 & qpp8_1 > 5 | qpp5_ches == 837 | qpp6_ches == 837 | qpp21_ches == 837, 1, 0)
EES_2014$lv_ches_815 <- ifelse(countrycode==1380 & qpp8_2 > 5 | qpp5_ches == 815 | qpp6_ches == 815 | qpp21_ches == 815, 1, 0)
EES_2014$lv_ches_811 <- ifelse(countrycode==1380 & qpp8_3 > 5 | qpp5_ches == 811 | qpp6_ches == 811 | qpp21_ches == 811, 1, 0)
EES_2014$lv_ches_845 <- ifelse(countrycode==1380 & qpp8_4 > 5 | qpp5_ches == 845 | qpp6_ches == 845 | qpp21_ches == 845, 1, 0)
EES_2014$lv_ches_814 <- ifelse(countrycode==1380 & qpp8_5 > 5 | qpp5_ches == 814 | qpp6_ches == 814 | qpp21_ches == 814, 1, 0)
EES_2014$lv_ches_838 <- ifelse(countrycode==1380 & qpp8_6 > 5 | qpp5_ches == 838 | qpp6_ches == 838 | qpp21_ches == 838, 1, 0)
EES_2014$lv_ches_848 <- ifelse(countrycode==1380 & qpp8_7 > 5 | qpp5_ches == 848 | qpp6_ches == 848 | qpp21_ches == 848, 1, 0)
EES_2014$lv_ches_844 <- ifelse(countrycode==1380 & qpp8_8 > 5 | qpp5_ches == 844 | qpp6_ches == 844 | qpp21_ches == 844, 1, 0)

EES_2014$chess_8_1[countrycode==1380] <- 837
EES_2014$chess_8_2[countrycode==1380] <- 815
EES_2014$chess_8_3[countrycode==1380] <- 811
EES_2014$chess_8_4[countrycode==1380] <- 845
EES_2014$chess_8_5[countrycode==1380] <- 814
EES_2014$chess_8_6[countrycode==1380] <- 838
EES_2014$chess_8_7[countrycode==1380] <- 848
EES_2014$chess_8_8[countrycode==1380] <- 844


#Luxembourg
EES_2014$lv_ches_3801 <- ifelse(countrycode==1442 & qpp8_1 > 5 | qpp5_ches == 3801 | qpp6_ches == 3801 | qpp21_ches == 3801, 1, 0)
EES_2014$lv_ches_3804 <- ifelse(countrycode==1442 & qpp8_2 > 5 | qpp5_ches == 3804 | qpp6_ches == 3804 | qpp21_ches == 3804, 1, 0)
EES_2014$lv_ches_3803 <- ifelse(countrycode==1442 & qpp8_3 > 5 | qpp5_ches == 3803 | qpp6_ches == 3803 | qpp21_ches == 3803, 1, 0)
EES_2014$lv_ches_3802 <- ifelse(countrycode==1442 & qpp8_4 > 5 | qpp5_ches == 3802 | qpp6_ches == 3802 | qpp21_ches == 3802, 1, 0)
EES_2014$lv_ches_3806 <- ifelse(countrycode==1442 & qpp8_5 > 5 | qpp5_ches == 3806 | qpp6_ches == 3806 | qpp21_ches == 3806, 1, 0)
EES_2014$lv_ches_3805 <- ifelse(countrycode==1442 & qpp8_6 > 5 | qpp5_ches == 3805 | qpp6_ches == 3805 | qpp21_ches == 3805, 1, 0)

EES_2014$chess_8_1[countrycode==1442] <- 3801
EES_2014$chess_8_2[countrycode==1442] <- 3804
EES_2014$chess_8_3[countrycode==1442] <- 3803
EES_2014$chess_8_4[countrycode==1442] <- 3802
EES_2014$chess_8_5[countrycode==1442] <- 3806
EES_2014$chess_8_6[countrycode==1442] <- 3805


#Netherlands
EES_2014$lv_ches_1003 <- ifelse(countrycode==1528 & qpp8_1 > 5 | qpp5_ches == 1003 | qpp6_ches == 1003 | qpp21_ches == 1003, 1, 0)
EES_2014$lv_ches_1002 <- ifelse(countrycode==1528 & qpp8_2 > 5 | qpp5_ches == 1002 | qpp6_ches == 1002 | qpp21_ches == 1002, 1, 0)
EES_2014$lv_ches_1017 <- ifelse(countrycode==1528 & qpp8_3 > 5 | qpp5_ches == 1017 | qpp6_ches == 1017 | qpp21_ches == 1017, 1, 0)
EES_2014$lv_ches_1014 <- ifelse(countrycode==1528 & qpp8_4 > 5 | qpp5_ches == 1014 | qpp6_ches == 1014 | qpp21_ches == 1014, 1, 0)
EES_2014$lv_ches_1001 <- ifelse(countrycode==1528 & qpp8_5 > 5 | qpp5_ches == 1001 | qpp6_ches == 1001 | qpp21_ches == 1001, 1, 0)
EES_2014$lv_ches_1004 <- ifelse(countrycode==1528 & qpp8_6 > 5 | qpp5_ches == 1004 | qpp6_ches == 1004 | qpp21_ches == 1004, 1, 0)
EES_2014$lv_ches_1016 <- ifelse(countrycode==1528 & qpp8_7 > 5 | qpp5_ches == 1016 | qpp6_ches == 1016 | qpp21_ches == 1016, 1, 0)
EES_2014$lv_ches_1005 <- ifelse(countrycode==1528 & qpp8_8 > 5 | qpp5_ches == 1005 | qpp6_ches == 1005 | qpp21_ches == 1005, 1, 0)

EES_2014$chess_8_1[countrycode==1528] <- 1003
EES_2014$chess_8_2[countrycode==1528] <- 1002
EES_2014$chess_8_3[countrycode==1528] <- 1017
EES_2014$chess_8_4[countrycode==1528] <- 1014
EES_2014$chess_8_5[countrycode==1528] <- 1001
EES_2014$chess_8_6[countrycode==1528] <- 1004
EES_2014$chess_8_7[countrycode==1528] <- 1016
EES_2014$chess_8_8[countrycode==1528] <- 1005


#Portugal
EES_2014$lv_ches_1206 <- ifelse(countrycode==1620 & qpp8_1 > 5 | qpp5_ches == 1206 | qpp6_ches == 1206 | qpp21_ches == 1206, 1, 0)
EES_2014$lv_ches_1202 <- ifelse(countrycode==1620 & qpp8_2 > 5 | qpp5_ches == 1202 | qpp6_ches == 1202 | qpp21_ches == 1202, 1, 0)
EES_2014$lv_ches_1205 <- ifelse(countrycode==1620 & qpp8_3 > 5 | qpp5_ches == 1205 | qpp6_ches == 1205 | qpp21_ches == 1205, 1, 0)
EES_2014$lv_ches_1201 <- ifelse(countrycode==1620 & qpp8_4 > 5 | qpp5_ches == 1201 | qpp6_ches == 1201 | qpp21_ches == 1201, 1, 0)
EES_2014$lv_ches_1208 <- ifelse(countrycode==1620 & qpp8_5 > 5 | qpp5_ches == 1208 | qpp6_ches == 1208 | qpp21_ches == 1208, 1, 0)
EES_2014$lv_ches_1209 <- ifelse(countrycode==1620 & qpp8_6 > 5 | qpp5_ches == 1209 | qpp6_ches == 1209 | qpp21_ches == 1209, 1, 0)

EES_2014$chess_8_1[countrycode==1620] <- 1206
EES_2014$chess_8_2[countrycode==1620] <- 1202
EES_2014$chess_8_3[countrycode==1620] <- 1205
EES_2014$chess_8_4[countrycode==1620] <- 1201
EES_2014$chess_8_5[countrycode==1620] <- 1208
EES_2014$chess_8_6[countrycode==1620] <- 1209


#Sweden
EES_2014$lv_ches_1602 <- ifelse(countrycode==1752 & qpp8_1 > 5 | qpp5_ches == 1602 | qpp6_ches == 1602 | qpp21_ches == 1602, 1, 0)
EES_2014$lv_ches_1605 <- ifelse(countrycode==1752 & qpp8_2 > 5 | qpp5_ches == 1605 | qpp6_ches == 1605 | qpp21_ches == 1605, 1, 0)
EES_2014$lv_ches_1607 <- ifelse(countrycode==1752 & qpp8_3 > 5 | qpp5_ches == 1607 | qpp6_ches == 1607 | qpp21_ches == 1607, 1, 0)
EES_2014$lv_ches_1604 <- ifelse(countrycode==1752 & qpp8_4 > 5 | qpp5_ches == 1604 | qpp6_ches == 1604 | qpp21_ches == 1604, 1, 0)
EES_2014$lv_ches_1603 <- ifelse(countrycode==1752 & qpp8_5 > 5 | qpp5_ches == 1603 | qpp6_ches == 1603 | qpp21_ches == 1603, 1, 0)
EES_2014$lv_ches_1610 <- ifelse(countrycode==1752 & qpp8_6 > 5 | qpp5_ches == 1610 | qpp6_ches == 1610 | qpp21_ches == 1610, 1, 0)
EES_2014$lv_ches_1606 <- ifelse(countrycode==1752 & qpp8_7 > 5 | qpp5_ches == 1606 | qpp6_ches == 1606 | qpp21_ches == 1606, 1, 0)
EES_2014$lv_ches_1601 <- ifelse(countrycode==1752 & qpp8_8 > 5 | qpp5_ches == 1601 | qpp6_ches == 1601 | qpp21_ches == 1601, 1, 0)

EES_2014$chess_8_1[countrycode==1752] <- 1602
EES_2014$chess_8_2[countrycode==1752] <- 1605
EES_2014$chess_8_3[countrycode==1752] <- 1607
EES_2014$chess_8_4[countrycode==1752] <- 1604
EES_2014$chess_8_5[countrycode==1752] <- 1603
EES_2014$chess_8_6[countrycode==1752] <- 1610
EES_2014$chess_8_7[countrycode==1752] <- 1606
EES_2014$chess_8_8[countrycode==1752] <- 1601


#UK
EES_2014$lv_ches_1101 <- ifelse(countrycode==1826 & qpp8_1 > 5 | qpp5_ches == 1101 | qpp6_ches == 1101 | qpp21_ches == 1101, 1, 0)
EES_2014$lv_ches_1102 <- ifelse(countrycode==1826 & qpp8_2 > 5 | qpp5_ches == 1102 | qpp6_ches == 1102 | qpp21_ches == 1102, 1, 0)
EES_2014$lv_ches_1104 <- ifelse(countrycode==1826 & qpp8_3 > 5 | qpp5_ches == 1104 | qpp6_ches == 1104 | qpp21_ches == 1104, 1, 0)
EES_2014$lv_ches_1107 <- ifelse(countrycode==1826 & qpp8_4 > 5 | qpp5_ches == 1107 | qpp6_ches == 1107 | qpp21_ches == 1107, 1, 0)
EES_2014$lv_ches_1108 <- ifelse(countrycode==1826 & qpp8_5 > 5 | qpp5_ches == 1108 | qpp6_ches == 1108 | qpp21_ches == 1108, 1, 0)
EES_2014$lv_ches_1105 <- ifelse(countrycode==1826 & qpp8_6 > 5 | qpp5_ches == 1105 | qpp6_ches == 1105 | qpp21_ches == 1105, 1, 0)
EES_2014$lv_ches_1106 <- ifelse(countrycode==1826 & qpp8_7 > 5 | qpp5_ches == 1106 | qpp6_ches == 1106 | qpp21_ches == 1106, 1, 0)

EES_2014$chess_8_1[countrycode==1826] <- 1101
EES_2014$chess_8_2[countrycode==1826] <- 1102
EES_2014$chess_8_3[countrycode==1826] <- 1104
EES_2014$chess_8_4[countrycode==1826] <- 1107
EES_2014$chess_8_5[countrycode==1826] <- 1108
EES_2014$chess_8_6[countrycode==1826] <- 1105
EES_2014$chess_8_7[countrycode==1826] <- 1106

detach(EES_2014)





######### Code-up MII ------------------------

# first issue qpp1aO_EMCS  # second issue  qpp1bO_EMCS

EES_2014$qpp1aO_EMCS[EES_2014$qpp1aO_EMCS < 0] <- NA
EES_2014$qpp1aO_EMCS[EES_2014$qpp1bO_EMCS < 0] <- NA



#immigration as mii
EES_2014$mii_immi <- NA
EES_2014$mii_immi[EES_2014$qpp1aO_EMCS == 80505 | EES_2014$qpp1aO_EMCS == 80506 | 
                    EES_2014$qpp1aO_EMCS == 80502 | EES_2014$qpp1aO_EMCS == 80501 | 
                    EES_2014$qpp1aO_EMCS == 80100 | EES_2014$qpp1aO_EMCS == 60807 | 
                    EES_2014$qpp1aO_EMCS == 60808 | EES_2014$qpp1aO_EMCS == 60809 |
                    EES_2014$qpp1aO_EMCS == 60803] <- 1
EES_2014$mii_immi[EES_2014$qpp1bO_EMCS == 80505 | EES_2014$qpp1bO_EMCS == 80506 | 
                    EES_2014$qpp1bO_EMCS == 80502 | EES_2014$qpp1bO_EMCS == 80501 | 
                    EES_2014$qpp1bO_EMCS == 80100 | EES_2014$qpp1bO_EMCS == 60807 | 
                    EES_2014$qpp1bO_EMCS == 60808 | EES_2014$qpp1bO_EMCS == 60809 |
                    EES_2014$qpp1bO_EMCS == 60803] <- 1
EES_2014$mii_immi[!is.na(EES_2014$qpp1aO_EMCS) & is.na(EES_2014$mii_immi)] <- 0






######### Pivot EES from wide to long ------------------------
#### Pivot in two steps 
## Select the columns with party thermostats
pivotvars <- EES_2014 %>% dplyr::select(respid, #res ID 
                                 qpp8_1, qpp8_2, qpp8_3, qpp8_4, qpp8_5, qpp8_6,
                                 qpp8_7, qpp8_8)

## Pivot the party names
data1 <- pivotvars %>% pivot_longer(-respid, names_to = "partynumber", values_to="party_therm")
data1$partynumber <- str_sub(data1$partynumber, start= -1)


## Select the columns with CHES IDS
pivotvars <- EES_2014 %>% dplyr::select(respid, #res ID 
                                 chess_8_1, chess_8_2, chess_8_3, chess_8_4,
                                 chess_8_5, chess_8_6, chess_8_7, chess_8_8)

## Pivot the CHES IDS
data2 <- pivotvars %>% pivot_longer(-respid, names_to = "partynumber", values_to="party_id")
data2$partynumber <- str_sub(data2$partynumber, start= -1)

long_data <- full_join(data1, data2)  %>% dplyr::select(-partynumber)
rm(pivotvars, data1, data2)


# Clean the variable
long_data$party_therm[long_data$party_therm < 0] <- NA




######### Select useful in EES variables ------------------------
## Choose respondent characteristics to add
reschar <- EES_2014 %>% dplyr::select(respid, qpp1aO_EES, qpp1aO_EMCS, qpp1bO_EES, qpp1bO_EMCS, mii_immi,
                               q1, d8, d10, vd11, c14, d25, d60,
                               qpp17_6) %>%
  rename(citizen=q1,
         educationage=d8,
         gender=d10,
         age=vd11,
         occupation=c14,
         urban=d25,
         difficultybill=d60,
         immigration_at=qpp17_6)


# Add them to the long data
long_data <- left_join(long_data, reschar)

















######### Add CHES data ------------------------

ches_2014_expert <- read_dta("data/2014_CHES_dataset_expert-level.dta")



##First for 2014 expert level data
###Adding expert SD for  Left Right
ches_trend$lrecon_sd <- NA
for (i in ches_trend$party_id ) {
  ches_trend$lrecon_sd[ches_trend$party_id==i & ches_trend$year == 2014] = sd(ches_2014_expert$lrecon[ches_2014_expert$party_id==i],na.rm=T)}

###Adding expert SD for galtan
ches_trend$galtan_sd <- NA
for (i in ches_trend$party_id ) {
  ches_trend$galtan_sd[ches_trend$party_id==i & ches_trend$year == 2014] = sd(ches_2014_expert$galtan[ches_2014_expert$party_id==i],na.rm=T)}

###Adding expert SD for EU position
ches_trend$eu_dimension_sd <- NA
for (i in ches_trend$party_id ) {
  ches_trend$eu_dimension_sd[ches_trend$party_id==i & ches_trend$year == 2014] = sd(ches_2014_expert$eu_position[ches_2014_expert$party_id==i],na.rm=T)}

###Adding expert SD for immigration position
ches_trend$immigration_dimension_sd <- NA
for (i in ches_trend$party_id ) {
  ches_trend$immigration_dimension_sd[ches_trend$party_id==i & ches_trend$year == 2014] = sd(ches_2014_expert$immigrate_policy[ches_2014_expert$party_id==i],na.rm=T)}









### Merge everything together 
long_data <- left_join(long_data, ches_trend)
rm(ches_2014_expert)







################# prepare control variables ----------------------
long_data$immigration_at[long_data$immigration_at<0] <- NA   # 0 You are fully in favour of the redistribution of wealth from the rich to the poor in (OUR COUNTRY)
long_data$educationage[long_data$educationage < 0] <- NA
long_data$urban[long_data$urban < 0] <- NA
long_data$difficultybill[long_data$difficultybill < 0] <- NA
long_data$educationage_fac <- as.factor(long_data$educationage)







################# Analysis for the voter section ----------------------

# With controls
fullmod <- lm_robust(party_therm~mii_immi*immigration_dimension_sd + 
            immigration_at + immigrate_policy + #position characteristics
            gender +  educationage_fac + #gender is female
            urban + difficultybill +
            as.factor(country),  
            data=subset(long_data, year==2014),  
            cluster=respid) 
summary(fullmod)

# With binned-up version of the SD variable to show that the results are not a fluke
long_data <- long_data %>%
  mutate(immigration_dimension_sd_4 = as.factor(ntile(immigration_dimension_sd, 4)))

fullmod4 <- lm_robust(party_therm~mii_immi*immigration_dimension_sd_4 + 
                       immigration_at + immigrate_policy + #position characteristics
                       gender +  educationage_fac + #gender is female
                       urban + difficultybill,
                       fixed_effects=country,
                       data=subset(long_data, year==2014),  
                       cluster=respid) 



# Get the predictions
preds <- ggpredict(fullmod, terms=c("mii_immi [0,1]", "immigration_dimension_sd [0:3]"))
preds$x <- as.character(preds$x)
preds$x[preds$x=="1"] <- "Immigration is important"
preds$x[preds$x=="0"] <- "Immigration is not important"


## Plot
plot <- ggplot() + 
  geom_pointrange(data=preds, aes(y=predicted, x=group, color=x, shape=x, ymin=conf.low, ymax=conf.high), size=0.9, linewidth=1,
                  position = position_dodge2(width=0.3))  + 
  theme(axis.text=element_text(size=11), legend.text=element_text(size=12), legend.position = "bottom") +
  scale_color_manual(values=c("#51127c", "#fc8961")) +
  labs(y="Predicted position on party thermostat (0-10)",
       x="Party ambiguity on immigration (0-3.5)",
       shape="",
       color="")

plot
ggsave(plot=plot, "plots/fig9_main_vot.png", dpi = 300, width=6, height=5)
ggsave(plot=plot, "plots/fig9_main_vot.tiff", dpi = 300, width=6, height=5, device="tiff")
#ggsave(plot=plot, "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/plots/std3_main_vot.png", dpi = 300,  width=6, height=5)




## Create a table (Table A8)
table <- texreg(l=list(fullmod, fullmod4),
                use.packages=F,
                stars=c(0.05, 0.01, 0.001), 
                digits=3, include.ci = FALSE, booktabs=T,
                custom.header=list("DV: Party thermostat"=1:2),
                custom.model.names = c("Linear interaction", "Binned interaction"),
                caption.above=T,
                label="std3_main_vot",
                caption="Effect of blurring and issue importance on liking parties",
                threeparttable = TRUE,
                float.pos="htpb!",
                omit.coef=c("(country)"),
                custom.coef.names = c("Intercept", "Immigration MII", "Party immi ambiguity", "Immigration attitude", 
                                      "Party position", "Female", "Left school at 16-19", "Left school at 20+", "Still in school", "No education",
                                      "Urban (1-3)", "Easy paying bills (1-3)", "Immi MII X Party immi ambiguity", 
                                      "Party immi ambiguity Q2", "Party immi ambiguity Q3", "Party immi ambiguity Q4",
                                      "Immi MII X Party immi ambiguity Q2", "Immi MII X Party immi ambiguity Q3", "Immi MII X Party immi ambiguity Q4"),
                custom.note="\\item %stars. Models are estimated through OLS with SEs clustered at the individual level and including country FEs. The baseline for education is leaving education at age 15. The baseline for the party ambiguity variable is the first quartile of the variable.")

print(table, file = "tables/std3_main_vot.tex")
#print(table, file = "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/tables/std3_main_vot.tex")












################# More cleaning for the party section ----------------------
## Change the data to a long format after selecting right variables

long_data_forp <- EES_2014 %>% dplyr::select(respid, mii_immi, qpp17_6,
                                      starts_with('lv_ches', ignore.case = TRUE)) %>%
  rename(immigration_at=qpp17_6) %>% pivot_longer(names_to="party_id", 
                                       cols= starts_with("lv_ches_"),
                                       names_prefix = "lv_ches_",
                                       values_to = "likelyvoter")


long_data_forp$immigration_at[long_data_forp$immigration_at < 0] <- NA




# Get the party-level information
long_data_forp <- long_data_forp %>%  
  filter(likelyvoter==1) %>% #only keep likely voters 
  group_by(party_id) %>% 
  mutate(number_voters = n(), #number of voters per party. Median is 372
         mean_mii_immi = mean(mii_immi,na.rm=T),
         sd_immi = sd(immigration_at,na.rm=T)) %>%
  ungroup() %>%
  distinct(party_id, .keep_all=T) %>%
  dplyr::select(party_id, number_voters, sd_immi, mean_mii_immi) %>%
  mutate(party_id=as.numeric(party_id),
         mean_mii_immi = mean_mii_immi * 100)


ches_trend <- left_join(ches_trend, long_data_forp)



################# Analysis for the party section ----------------------
#### Create a plot

# Add which parties to highlight
ches_trend$highlight <- NA
ches_trend$highlight[ches_trend$party=="VVD" | 
                       ches_trend$party=="PvdA" |
                       ches_trend$party=="PVV" |
                       ches_trend$party=="GL" |
                       ches_trend$party=="CDA"] <- 1
ches_trend$highlight[is.na(ches_trend$highlight)] <- 0
ches_trend$highlight <- as.factor(ches_trend$highlight)


# Needed for regression equation
df <- ches_trend %>% dplyr::select(immigration_dimension_sd, mean_mii_immi) %>%
  mutate(y=immigration_dimension_sd,
         x=mean_mii_immi)

lm_eqn <- function(df){
  m <- lm(y ~ x, df);
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
                   list(a = format(unname(coef(m)[1]), digits = 2),
                        b = format(unname(coef(m)[2]), digits = 2),
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));
}


# The actual plot
plot <- ches_trend %>% filter(year==2014) %>% 
  ggplot() + 
  geom_smooth(aes(x=mean_mii_immi, y=immigration_dimension_sd), method="lm", color="#51127c") + 
  geom_point(aes(x=mean_mii_immi, y=immigration_dimension_sd, color=highlight), alpha=0.8, size=2.2) +
  geom_point(data=subset(ches_trend, highlight== 1 & year == 2014), aes(x=mean_mii_immi, y=immigration_dimension_sd),
             color="#fc8961", size=3.5) + 
  labs(x="Percentage of likely voters who find immigration important", 
       y="Party ambiguity on immigration") +
  scale_x_continuous(labels = function(x) paste0(x, "%")) +  
  geom_text(x = 52, y = 3.3, label = lm_eqn(df), parse = TRUE, size=4.2) +
  geom_label_repel(data=subset(ches_trend, highlight== 1 & year == 2014),
            aes(mean_mii_immi,immigration_dimension_sd,label=party)) +
  theme(axis.text=element_text(size=11), legend.text=element_text(size=12), legend.position = "none") +
  scale_color_manual(values=c("darkgrey", "#fc8961"))


plot

## Depending on the R version this may not completely do the plot formatting right
ggsave(plot=plot, "plots/fig10_main_par.png", dpi = 300, width=6, height=5)
ggsave(plot=plot, "plots/fig10_main_par.tiff", dpi = 300, width=6, height=5, device="tiff")
#ggsave(plot=plot, "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/plots/std3_main_par.png", dpi = 300,  width=6, height=5)



  